package xyz.bali16.module.invitecode.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import xyz.bali16.application.core.utils.TimeUtil;
import xyz.bali16.module.invitecode.entity.InviteCode;
import xyz.bali16.module.invitecode.model.invitecode.InviteCodeAddParam;
import xyz.bali16.module.invitecode.model.invitecode.InviteCodePageParam;
import xyz.bali16.module.invitecode.model.invitecode.InviteCodeUpdateParam;
import xyz.bali16.module.invitecode.entity.InviteCode;
import xyz.bali16.module.invitecode.mapper.InviteCodeMapper;
import xyz.bali16.module.invitecode.service.InviteCodeService;


/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@Service
public class InviteCodeServiceImpl extends ServiceImpl<InviteCodeMapper, InviteCode> implements InviteCodeService {
    private final InviteCodeMapper inviteCodeMapper;

    public InviteCodeServiceImpl(InviteCodeMapper inviteCodeMapper) {
        this.inviteCodeMapper = inviteCodeMapper;
    }

    @Override
    public IPage<InviteCode> getPage(InviteCodePageParam param) {
        IPage<?> page = new Page<>(param.getCurrent(), param.getSize());
        return this.baseMapper.getPage(page, param.getInvitationCode(),param.getIsUsed());
    }

    @Override
    public void addInviteCode(InviteCodeAddParam param) throws Exception {
        Preconditions.checkNotNull(param, "InviteCodeAddParam 参数不能为为空");
        InviteCode invitation = new InviteCode();
        invitation.setGmtCreated(TimeUtil.getNowTimeToSeconds());
        BeanUtils.copyProperties(param, invitation);
        System.out.println(invitation);
        save(invitation);
    }

    @Override
    public void updateInviteCode(InviteCodeUpdateParam param) throws Exception {
        Preconditions.checkNotNull(param, "InviteCodeUpdateParam 参数不能为为空");
        InviteCode invitation = new InviteCode();
        BeanUtils.copyProperties(param, invitation);
        updateById(invitation);
    }


    @Override
    public void delete(String linkId) throws Exception {
        if (linkId == null) {
            return;
        }
        removeById(linkId);

    }
}
